package com.yunhe.fountain.task;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.fountain.domain.Fountain;
import com.yunhe.fountain.scoket.CardFlow;
import com.yunhe.fountain.service.FountainControlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import com.yunhe.common.util.CollectionUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TaskController
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/20 16:57
 * @Version 1.0
 **/
@Api(value = "FountainTask",description = "定时下发命令控制喷泉",tags = "{Fountain_Task}")
@RestController
@RequestMapping(value = "/fountainTask")
@EnableScheduling
public class FountainTaskController {

    private static Logger log = LoggerFactory.getLogger(FountainTaskController.class);

    @Autowired
    private  FountainControlService fountainControlService;

    @ApiOperation("8点30分定时下发继续命令")
    @RequestMapping(value = "/issueContinueCommand",method = RequestMethod.POST)
    @Scheduled(cron = "0 30 7 * * ? ")
    @Async
    public ResponseEntity<ResultObject> issueContinueCommand() {
        log.info("8点30分定时下发继续命令定时任务开始");
        List<Fountain> fountainList = fountainControlService.getFountainControl();
        String format = DateTimeUtils.format(DateTimeUtils.PATTERN.DATETIME.value(), new Date());
        if(!CollectionUtil.isEmpty(fountainList)){
            for (Fountain fountain : fountainList) {
                try {
                    String s = CardFlow.issueAnOrder(fountain.getFountainIp(), Integer.valueOf(fountain.getFountainPort()), "0200");
                    log.info(s);
                    log.info("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"8点30分定时开启成功");
                } catch (ArgumentErrorException e) {
                    e.printStackTrace();
                    log.error("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"8点30分定时开启失败");
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"8点30分定时开启失败");
                }
            }
        }
        log.info("8点30分定时下发继续命令定时任务结束");
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("17点定时下发继续命令")
    @RequestMapping(value = "/issueContinueCommand1",method = RequestMethod.POST)
    @Scheduled(cron = "0 0 17 * * ?")
    @Async
    public ResponseEntity<ResultObject> issueContinueCommand1() {
        log.info("17点定时下发继续命令定时任务开始");
        List<Fountain> fountainList = fountainControlService.getFountainControl();
        String format = DateTimeUtils.format(DateTimeUtils.PATTERN.DATETIME.value(), new Date());
        if(!CollectionUtil.isEmpty(fountainList)){
            for (Fountain fountain : fountainList) {
                try {
                    String s = CardFlow.issueAnOrder(fountain.getFountainIp(), Integer.valueOf(fountain.getFountainPort()), "0200");
                    log.info(s);
                    log.info("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"17点定时开启成功");
                } catch (ArgumentErrorException e) {
                    e.printStackTrace();
                    log.error("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"17点定时开启失败");
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"17点定时开启失败");
                }
            }
        }
        log.info("17点定时下发继续命令定时任务结束");
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }


    @ApiOperation("9点30分定时下发停止命令")
    @RequestMapping(value = "/issueStopCommand",method = RequestMethod.POST)
    @Scheduled(cron = "0 0 9 * * ?")
    @Async
    public ResponseEntity<ResultObject> issueStopCommand()  {
        log.info("9点30分定时下发停止命令定时任务开始");
        List<Fountain> fountainList = fountainControlService.getFountainControl();
        String format = DateTimeUtils.format(DateTimeUtils.PATTERN.DATETIME.value(), new Date());
        if(!CollectionUtil.isEmpty(fountainList)){
            for (Fountain fountain : fountainList) {
                try {
                    String s = CardFlow.issueAnOrder(fountain.getFountainIp(),Integer.valueOf(fountain.getFountainPort()),"0000");
                    log.info(s);
                    log.info("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"9点30分定时关闭成功");
                } catch (ArgumentErrorException e) {
                    e.printStackTrace();
                    log.error("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"9点30分定时关闭失败");
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"9点30分定时关闭失败");
                }

            }
        }
        log.info("9点30分定时下发停止命令定时任务结束");
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("19点定时下发停止命令")
    @RequestMapping(value = "/issueStopCommand1",method = RequestMethod.POST)
    @Scheduled(cron = "0 0 19 * * ?")
    @Async
    public ResponseEntity<ResultObject> issueStopCommand1()  {
        log.info("19点定时下发停止命令定时任务开始");
        List<Fountain> fountainList = fountainControlService.getFountainControl();
        String format = DateTimeUtils.format(DateTimeUtils.PATTERN.DATETIME.value(), new Date());
        if(!CollectionUtil.isEmpty(fountainList)){
            for (Fountain fountain : fountainList) {
                try {
                    String s = CardFlow.issueAnOrder(fountain.getFountainIp(),Integer.valueOf(fountain.getFountainPort()),"0000");
                    log.info(s);
                    log.info("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"19点定时关闭成功");
                } catch (ArgumentErrorException e) {
                    e.printStackTrace();
                    log.error("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"19点定时关闭失败");
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("喷泉ip："+fountain.getFountainIp()+"端口："+fountain.getFountainPort()+"当前时间："+format+"19点定时关闭失败");
                }

            }
        }
        log.info("19点定时下发停止命令定时任务结束");
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }


}
